# @Time    : 2021/1/19 14:06
# @Author  : LDY
# @Email   : 274884591@qq.com
# @File    : Decorator.py
# @desc    : 各类装饰器
# @Software: PyCharm
from functools import wraps

from selenium.webdriver.support import expected_conditions as EC
from Base.exceptionsDIY import locationException


# 元素定位装饰器，增加元素等待和异常判断
def get_parameters(by, content):
    def locate_outer(func):
        @wraps(func)
        def locate_inner(self, *args, **kwargs):
            if by in ["id", "xpath", "link text", "partial link text",
                      "name", "tag name", "class name", "css selector"]:
                try:
                    element = self.wait.until(EC.presence_of_element_located((by, content)))
                except Exception as e:
                    raise locationException(f'定位元素失败 根据{by}，无法找到{content}元素')
                return func(self, element, *args, **kwargs)
            print('''请输入"id", "xpath", "link text", "partial link text", "name", "tag name", "class name", 
                                        "css selector"''')

        return locate_inner

    return locate_outer
